#include "user_can.h"

/* USER CODE BEGIN 0 */
#include "string.h"
#include "crc.h"
uint8_t canRXflag=0;
FDCAN_TxHeaderTypeDef TxHeader={0};
FDCAN_TxHeaderTypeDef TxHeader2={0};
FDCAN_TxHeaderTypeDef TxHeader3={0};
/* USER CODE END 0 */

/* USER CODE BEGIN 1 */
//*****************************************************************My****************************************************************************//
static FDCAN_RxHeaderTypeDef RxHeader;
static FDCAN_RxHeaderTypeDef RxHeader2;
static FDCAN_RxHeaderTypeDef RxHeader3;
void canfd_Filterinit(FDCAN_HandleTypeDef *hfdcan)
{
    FDCAN_FilterTypeDef sFilter={0};
    sFilter.FilterConfig=FDCAN_FILTER_TO_RXFIFO0;
    sFilter.FilterID1=0x000;
    sFilter.FilterID2=0x7FF;
    sFilter.FilterIndex=0;
    sFilter.FilterType=FDCAN_FILTER_MASK;
    sFilter.IdType=FDCAN_STANDARD_ID; 
    
    if (HAL_FDCAN_ConfigFilter(hfdcan, &sFilter) != HAL_OK)
  {
    Error_Handler();
  }
  
  if (HAL_FDCAN_ActivateNotification(hfdcan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0) != HAL_OK)
  {
    Error_Handler();
  }
//   if (HAL_FDCAN_ConfigGlobalFilter(hfdcan, FDCAN_REJECT, FDCAN_REJECT, FDCAN_FILTER_REMOTE, FDCAN_REJECT_REMOTE) != HAL_OK)
//  {
//    Error_Handler();
//  }
    if (HAL_FDCAN_ActivateNotification(hfdcan, FDCAN_IT_BUS_OFF, 0) != HAL_OK)
  {
    Error_Handler();
  }
  HAL_FDCAN_ConfigTxDelayCompensation(hfdcan, (*hfdcan).Init.DataPrescaler * (*hfdcan).Init.DataTimeSeg1, 0);
  HAL_FDCAN_EnableTxDelayCompensation(hfdcan);
    if (HAL_FDCAN_Start(hfdcan) != HAL_OK)
  {
    Error_Handler();
  }
}

//uint8_t brain_msg[16] = {0x7F, 0x06, 0x04, 0x4A, 0x06, 0x01, 0x61, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
//uint8_t brain_msg[16] = {0x7F, 0x10, 0x04, 0x2E, 0x00, 0x03, 0x06, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0xDB, 0xE0, 0x00};
uint8_t brain_msg[16] = {0x7E, 0x10, 0x04, 0x2E, 0x00, 0x03, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xDB, 0xE0, 0x00};
uint8_t brain_msgR[16] = {0x7F, 0x10, 0x04, 0x2E, 0x00, 0x03, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xDB, 0xE0, 0x00};
void CANFD_SendMsg(FDCAN_HandleTypeDef *hfdcan, uint32_t msgID,uint32_t DataLength,uint8_t *Data)
{
    if (msgID == 6) {
        TxHeader.BitRateSwitch=FDCAN_BRS_OFF; 
        TxHeader.DataLength=FDCAN_DLC_BYTES_16;
        TxHeader.ErrorStateIndicator=FDCAN_ESI_PASSIVE;
        TxHeader.FDFormat=FDCAN_FD_CAN;//CANFD
        TxHeader.Identifier=0x007E000F;//CAN ID
        TxHeader.IdType=FDCAN_EXTENDED_ID;//��׼ID
        TxHeader.MessageMarker=0;
        TxHeader.TxEventFifoControl=FDCAN_NO_TX_EVENTS;
        TxHeader.TxFrameType=FDCAN_DATA_FRAME;

        brain_msg[7] = Data[2];
        brain_msg[8] = Data[1];
        brain_msg[9] = Data[4];
        brain_msg[10] = Data[3];
        brain_msg[11] = Data[6];
        brain_msg[12] = Data[5];

        brain_msg[13]=CRC16(brain_msg,13);
        brain_msg[14]=CRC16(brain_msg,13)>>8;
    //	while(HAL_FDCAN_GetTxFifoFreeLevel(&hfdcan1) == 0);
        HAL_FDCAN_AddMessageToTxFifoQ(hfdcan,&TxHeader,brain_msg);
    } else if (msgID == 7) {
        TxHeader.BitRateSwitch=FDCAN_BRS_OFF; 
        TxHeader.DataLength=FDCAN_DLC_BYTES_16;
        TxHeader.ErrorStateIndicator=FDCAN_ESI_PASSIVE;
        TxHeader.FDFormat=FDCAN_FD_CAN;//CANFD
        TxHeader.Identifier=0x007F000F;//CAN ID
        TxHeader.IdType=FDCAN_EXTENDED_ID;//��׼ID
        TxHeader.MessageMarker=0;
        TxHeader.TxEventFifoControl=FDCAN_NO_TX_EVENTS;
        TxHeader.TxFrameType=FDCAN_DATA_FRAME;

        brain_msgR[7] = Data[2];
        brain_msgR[8] = Data[1];
        brain_msgR[9] = Data[4];
        brain_msgR[10] = Data[3];
        brain_msgR[11] = Data[6];
        brain_msgR[12] = Data[5];

        brain_msgR[13]=CRC16(brain_msgR,13);
        brain_msgR[14]=CRC16(brain_msgR,13)>>8;
    //	while(HAL_FDCAN_GetTxFifoFreeLevel(&hfdcan1) == 0);
        HAL_FDCAN_AddMessageToTxFifoQ(hfdcan,&TxHeader,brain_msgR);
    } else {
        TxHeader.BitRateSwitch=FDCAN_BRS_OFF; 
        TxHeader.DataLength=DataLength;
        TxHeader.ErrorStateIndicator=FDCAN_ESI_PASSIVE;
        TxHeader.FDFormat=FDCAN_CLASSIC_CAN;//CANFD
        TxHeader.Identifier=msgID;//CAN ID
        TxHeader.IdType=FDCAN_STANDARD_ID;//��׼ID
        TxHeader.MessageMarker=0;
        TxHeader.TxEventFifoControl=FDCAN_NO_TX_EVENTS;
        TxHeader.TxFrameType=FDCAN_DATA_FRAME;

    //	while(HAL_FDCAN_GetTxFifoFreeLevel(&hfdcan1) == 0);
        HAL_FDCAN_AddMessageToTxFifoQ(hfdcan,&TxHeader,Data);

    }
}


extern uint8_t CANFD_TXData1[8];

uint8_t RxData[8]={0};
uint8_t RxData2[8]={0};
uint8_t RxData3[8]={0};

uint8_t len=0;
uint8_t len2=0;
uint8_t len3=0;

uint8_t Rxid=0;
uint8_t Rxid2=0;
uint8_t Rxid3=0;

extern uint8_t err[24];
extern uint16_t f3;
extern uint8_t k[8];

uint8_t p_cnt=0;

void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs)
{   
     if((RxFifo0ITs & FDCAN_IT_RX_FIFO0_NEW_MESSAGE) != RESET)
        {
            if(hfdcan->Instance == FDCAN1)
            {
                  if(HAL_FDCAN_GetRxMessage(&hfdcan1, FDCAN_RX_FIFO0, &RxHeader, RxData)==HAL_OK)
                    {    
                         Rxid = (uint8_t)RxHeader.Identifier;    //从收到的消息中提取出ID
                         len = (uint8_t)RxHeader.DataLength;
                        recvf(&hfdcan1,Rxid);
//                        if((*p_point)!=0x0f)           //代表上位机有数据发送出去
//                         {
//                             //recvf(&hfdcan1,Rxid,*p_point);
//                             recvf(&hfdcan1,Rxid);
//                             p_point++;
//                             p_cnt++;
//                         }
//                         if(p_cnt>add_cnt-1)
//                         {
//                             for(uint8_t i=0;i<8;i++)
//                             {
//                                recv_cnt[i]=0x0f;
//                             }
//                             p_point = recv_cnt;
//                         }
                    }
            }

            if(hfdcan->Instance == FDCAN2)
            {
                if(HAL_FDCAN_GetRxMessage(&hfdcan2, FDCAN_RX_FIFO0, &RxHeader2, RxData2)==HAL_OK)
                {    

                     Rxid2 = (uint8_t)RxHeader2.Identifier;
                     len2 = (uint8_t)RxHeader2.DataLength;
                    recvf(&hfdcan2,Rxid2);
//                     if((*p_point)!=0x0f)
//                         {
//                             recvf(&hfdcan1,Rxid2);
//                             p_point++;
//                             p_cnt++;
//                         }
//                         if(p_cnt>add_cnt-1)
//                         {
//                             for(uint8_t i=0;i<8;i++)
//                             {
//                                recv_cnt[i]=0x0f;
//                             }
//                             p_point = recv_cnt;
//                         }
                }
            }

            if(hfdcan->Instance == FDCAN3)
            {
                if(HAL_FDCAN_GetRxMessage(&hfdcan3, FDCAN_RX_FIFO0, &RxHeader3, RxData3)==HAL_OK)
                {    
                   
                     Rxid3 = (uint8_t)RxHeader3.Identifier;
                     len3 = (uint8_t)RxHeader3.DataLength;
                    recvf(&hfdcan3,Rxid3);
//                     if((*p_point)!=0x0f)
//                         {
//                             recvf(&hfdcan1,Rxid3);
//                             p_point++;
//                             p_cnt++;
//                         }
//                         if(p_cnt>add_cnt-1)
//                         {
//                             for(uint8_t i=0;i<8;i++)
//                             {
//                                recv_cnt[i]=0x0f;
//                             }
//                             p_point = recv_cnt;
//                         }
                }
            }
        }
}


//void add_recv(void)
//{
//    add_cnt=0;    //cnt清0
//    
//    for(uint8_t i=0;i<8;i++)
//        {
//             if(k[i]==i)
//             {
//                 recv_cnt[add_cnt]=k[i];            //记录有几个电机可以接收到数据并回复
//                 add_cnt++;
//             }
//        }
//}



/* USER CODE END 1 */
